﻿#include "VAOneXMLAdaptorMaterialFoam.h"
#include "rapidxml.hpp"
#include "FITKAcousticsPhysics/FITKAcousticsPhysics.h"
#include "FITKAcousticsPhysics/FITKAcousticsMaterialsFoam.h"
#include "FITKAcousticsPhysics/FITKAcousticsAbsMaterial.h"
#include <QFile>
#include <iostream>



namespace IO
{
    bool VAOneXMLAdaptorMaterialFoam::adaptR()
    {
        // 返回操作是否成功
        return readMaterialFoam();
    }

    bool VAOneXMLAdaptorMaterialFoam::adaptW()
    {
        // 返回操作是否成功
        return writeMaterialFoam();
    }

    bool VAOneXMLAdaptorMaterialFoam::readMaterialFoam()
    {
        // 指针判断是否为空
        if(!_node)
        {
            return false;
        }

        if(!_dataObj)
        {
            return false;
        }

        auto nameNode = _node->first_node("Name")->first_node();

        if (nameNode == nullptr)
        {
            //材料数据中 名称为必要项 不可为空 并抛出异常日志
            consoleMessage(2, "MaterialFoam name is null");
            return false;
        }

        Acoustics::FITKAcousticsMaterialsFoam* foam = dynamic_cast<Acoustics::FITKAcousticsMaterialsFoam*>(_dataObj);

        if(!foam)
        {
            return false;
        }

        //数据读取状态 默认为true
        bool b = true;
        // 名称字符串
        QString strName;
        strName = readStrNodeValue(nameNode, &b);
        if(b)
        {
            foam->setDataObjectName(strName);
        }
        // 读取各项属性,每个属性可能存在缺省配置
        double dValue = readDoubleAttribute(_node, "density", &b);
        if(b)
        {
            foam->setDensity(dValue);
        }

        double frValue = readDoubleAttribute(_node, "flow_resistivity", &b);
        if(b)
        {
            foam->setFlowResistivity(frValue);
        }

        double porValue = readDoubleAttribute(_node, "porosity", &b);
        if(b)
        {
            foam->setPorosity(porValue);
        }

        double lrValue = readDoubleAttribute(_node, "loss_factor", &b);
        if(b)
        {
            foam->setLossFactor(lrValue);
        }

        double prValue = readDoubleAttribute(_node, "poisson_ratio", &b);
        if(b)
        {
            foam->setPoissonRatio(prValue);
        }

        double smValue = readDoubleAttribute(_node, "shear_modulus", &b);
        if(b)
        {
            foam->setshearModulus(smValue);
        }

        double tmValue = readDoubleAttribute(_node, "tensile_modulus", &b);
        if(b)
        {
            foam->setTensileModulus(tmValue);
        }

        double tlValue = readDoubleAttribute(_node, "thermal_length", &b);
        if(b)
        {
            foam->setThermalLength(tlValue);
        }

        double tValue = readDoubleAttribute(_node, "tortuosity", &b);
        if(b)
        {
            foam->setTortuosity(tValue);
        }

        double vlValue = readDoubleAttribute(_node, "viscous_length", &b);
        if(b)
        {
            foam->setViscousLength(vlValue);
        }

        // 读取备注
        auto noteNode = _node->first_node("Note");
        if(noteNode)
        {
            auto note = noteNode->first_node();

            if (note != nullptr)
            {
                QString strNote = readStrNodeValue(note, &b);

                if(b)
                {
                    foam->setMaterialDescribe(strNote);
                }
            }
        }

        //读取成功
        return true;
    }

    bool VAOneXMLAdaptorMaterialFoam::writeMaterialFoam()
    {
        // 指针判断是否为空
        if(!_node)
        {
            return false;
        }

        if(!_dataObj)
        {
            return false;
        }

        Acoustics::FITKAcousticsMaterialsFoam* foam = dynamic_cast<Acoustics::FITKAcousticsMaterialsFoam*>(_dataObj);

        // 判断指针
        if (!foam)
        {
            return false;
        }

        // 写入各项属性
        writeAttribute(_node, "density", foam->getDensity());
        writeAttribute(_node, "tensile_modulus", foam->getTensileModulus());
        writeAttribute(_node, "shear_modulus", foam->getshearModulus());
        writeAttribute(_node, "flow_resistivity", foam->getFlowResistivity());
        writeAttribute(_node, "poisson_ratio", foam->getPoissonRatio());
        writeAttribute(_node, "loss_factor", foam->getLossFactor());
        writeAttribute(_node, "porosity", foam->getPorosity());
        writeAttribute(_node, "tortuosity", foam->getTortuosity());
        writeAttribute(_node, "thermal_length", foam->getThermalLength());
        writeAttribute(_node, "viscous_length", foam->getViscousLength());
        // 创建名称节点
        createNodeElement(_node, "Name", foam->getDataObjectName());
        // 创建备注节点
        createNodeElement(_node, "Note", foam->getMaterialDescribe());
        //数据写入成功
        return true;
    }
}
